const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo:false,
    category2Name:""
  },
  category2NameInput:function(e){

    this.setData({
      category2Name: e.detail.value
    })
  },
  // 取消搜索,返回主页面
  search: function (e) {
    var that = this
    //页面初始化，option为页面跳转所带来的参数
    
    console.log("category2Name:" + that.data.category2Name )
    wx.request({
      url: app.globalData.baseUrl +'/category2/searchcategory2',
      method: 'GET',
      data: { "category2Name": that.data.category2Name },//传入的参数
      success: function (res) {
        var list = res.data.category2s;
        console.log("list:" + list)
        if (list == null) {
          var toastText = '获取数据失败' + res.data.errMsg;
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });
        } else {
          that.setData({
            category2s: list
          });
        }
      }
    })
  },

  //////授权
  login: function (e) {
    var that = this
    console.log(e);
    if (e.detail.errMsg.indexOf("ok") != -1) {
      //this.setData({ userInfo: e.detail.userInfo });
      //wx.setStorageSync("userInfo", e.detail.userInfo);//将对象存入本地缓存
      wx.setStorageSync("city", e.detail.userInfo.city);//将地址存入本地缓存
      console.log("这是.....：" + e.detail.userInfo.nickName);
      wx.login({
        success: function (res) {
          console.log("res...code..：" + res.code);
          //var code=res.code;
          wx.request({
            url: app.globalData.baseUrl + '/userinfo/login',
            method: "POST",
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {//获取数据传到后台
              code: res.code,
              avatarUrl: e.detail.userInfo.avatarUrl,
              userName: e.detail.userInfo.nickName,
              userGender: e.detail.userInfo.gender,
              userCity: e.detail.userInfo.city,
              userProvince: e.detail.userInfo.province,
              userCountry: e.detail.userInfo.country,
            },

            success: function (res) {
              console.log("这是....：" + res.data.userInfo.buyerOpenid);
              const user = res.data.userInfo
              ///
              if (user == null) {
                var toastText = '获取数据失败' + res.data.errMsg;
                wx.showToast({
                  title: toastText,
                  icon: '',
                  duration: 2000
                });
              } else {
                // 将返回的数据保存到全局的缓冲中，方便其他页面使用
                wx.setStorageSync("openid", user.buyerOpenid);//将对象存入本地缓存
                wx.setStorageSync("userInfo", user);//将对象存入本地缓存
                that.setData({ userInfo: user });

              }

              // 将返回的数据保存到全局的缓冲中，方便其他页面使用
              // wx.setStorageSync("openid", openid);//将对象存入本地缓存
            }
          })
        }
      })

    }

  },
  //////跳转订单页
  catchTapCategory2: function (e) {
    wx.setStorageSync("flag", false);
    var that = this;
    let openid = wx.getStorageSync("openid");
    console.log('openid:' + openid);
    var category2Id = e.currentTarget.dataset.category2id;
    console.log('category2Id:' + category2Id);
    wx.setStorageSync("category2ToOrder", category2Id);//将类型标号存入本地缓存
    wx.navigateTo({
      url: '../../test/test?category2id=' + category2Id + '&openid=' + openid,


    })



  },

  onLoad: function (options) {

    var that = this
    let userInfo = wx.getStorageSync("userInfo");
    
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          //app.globalData.isok = true;
          that.setData({ userInfo: userInfo });
          
        }
      }
    })






  }
})
